package com.ericsson.enap.modules.ebmLog.utils;

import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.enap.modules.ebmLog.constant.EbmConstant;
import com.ericsson.enap.modules.ebmLog.entity.EbmStatisticMapVO;
import com.ericsson.enap.modules.ebmLog.entity.EbmVO;
import org.slf4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: ebmLog功能工具类Util
 * @author: wangjingshi
 * @create: 2021-04-20 11:22
 */
public class EbmLogUtil {

    private Logger log = LoggerUtils.Logger(LogFileName.EbmLogJob);

    public static String subStringNum2(String lineTxt){
        return lineTxt.substring(lineTxt.indexOf(EbmConstant.EG) + EbmConstant.EBMNUMBER2);
    }

    public static String subStringNumK(String lineTxt){
        return lineTxt.substring(lineTxt.indexOf(EbmConstant.K));
    }

    public static String subStringNum3_k(String lineTxt){
        return lineTxt.substring(lineTxt.indexOf(EbmConstant.EG) + EbmConstant.EBMNUMBER3,lineTxt.indexOf(EbmConstant.K));
    }

    public static String subStringNum2_10(String lineTxt){
        return lineTxt.substring(lineTxt.indexOf(EbmConstant.EG) + EbmConstant.EBMNUMBER2,
                lineTxt.indexOf(EbmConstant.EG) + EbmConstant.EBMNUMBER10);
    }

    /**
     * 将字符串的首字母转大写
     * @param str 需要转换的字符串
     * @return
     */
    public static String captureName(String str) {
        // 进行字母的ascii编码前移，效率要高于截取字符串进行转换的操作
        char[] cs=str.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);
    }

    public static boolean matchNum2(String lineTxt){
        return (!"".equals(subStringNum2(lineTxt)));
    }

    public static boolean matchNum3_k(String lineTxt) { return (!"".equals(lineTxt.substring(lineTxt.indexOf(EbmConstant.EG) + EbmConstant.EBMNUMBER3,lineTxt.indexOf(EbmConstant.K)).trim()));}

    public static void doMmeStatisticAndBuildMap(EbmVO e, Map<String, EbmStatisticMapVO> map, String keyKey, String keyName){
        String key = e.getCc();
        if(keyKey!=null&&!keyKey.equals("")){
            key = key + "*" + e.getScc() + "*" + keyKey;
            EbmStatisticMapVO ebmStatisticMapVO;
            if(map.containsKey(key)){
                map.get(key).setK_value(map.get(key).getK_value()+1);
                switch(e.getEvent_result()){
                    case "reject":map.get(key).setK_reject(map.get(key).getK_reject()+1);
                        break;
                    case "abort":map.get(key).setK_abort(map.get(key).getK_abort()+1);
                        break;
                    case "ignore":map.get(key).setK_ignore(map.get(key).getK_ignore()+1);
                        break;
                }
            }else{
                ebmStatisticMapVO = new EbmStatisticMapVO();
                ebmStatisticMapVO.setK_name(keyName);
                ebmStatisticMapVO.setK_key(keyKey);
                ebmStatisticMapVO.setCc(e.getCc());
                ebmStatisticMapVO.setScc(e.getScc());
                ebmStatisticMapVO.setCcScc(e.getCc()+"-"+e.getScc());
                ebmStatisticMapVO.setCcDesc(e.getCcdesc());
                ebmStatisticMapVO.setSccDesc(e.getSccdesc());
                /*ebmStatisticMapVO.setNetid(e.getNetid());
                ebmStatisticMapVO.setDateTime(e.getDateTime());*/
                switch(e.getEvent_result()){
                    case "reject":ebmStatisticMapVO.setK_reject(1);ebmStatisticMapVO.setK_value(1);
                        break;
                    case "abort":ebmStatisticMapVO.setK_abort(1);ebmStatisticMapVO.setK_value(1);
                        break;
                    case "ignore":ebmStatisticMapVO.setK_ignore(1);ebmStatisticMapVO.setK_value(1);
                        break;
                }
                map.put(key, ebmStatisticMapVO);
            }
        }
    }

    /**
     * 根据类名，方法名，反射调用类的方法
     * @方法名称: getResult
     * @描述: TODO
     * @param className
     * @param methodName
     * @param params
     * @return
     */
    public static String getInvokeResult1(String className,String methodName,Map<String,Object> params){
        String result = null;
        try{
            Object obj = Class.forName(className).newInstance();
            Class clazz = obj.getClass();
            Method mothod;
            if(params!=null){
                mothod = clazz.getDeclaredMethod(methodName, Map.class);
                result = (String) mothod.invoke(obj, params);
            }else{
                mothod = clazz.getDeclaredMethod(methodName);
                result = (String) mothod.invoke(obj);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getInvokeResult(Object classObj,String methodName,Map<String,Object> params){
        String result = null;
        try{
            Class clazz = classObj.getClass();
            Method mothod;
            if(params!=null){
                mothod = clazz.getDeclaredMethod(methodName, Map.class);
                result = (String) mothod.invoke(classObj, params);
            }else{
                mothod = clazz.getDeclaredMethod(methodName);
                result = (String) mothod.invoke(classObj);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /*Person zhangsan = new Person();
    // void.class是方法返回的类型    String.class 是方法的入参类型
    MethodType mt = MethodType.methodType(void.class,String.class);
    MethodHandle handler = MethodHandles.lookup()

            .findVirtual(zhangsan.getClass(), "sing", mt)

            .bindTo(zhangsan);
        handler.invokeExact("Summer train ");*/


    public static void main(String[] args) {
        String className = "com.ericsson.enap.modules.ebmLog.entity.EbmVO";
        String methodName = "setAPN";
        String a = getInvokeResult1(className,methodName,null);
        System.out.println(a);
        EbmVO ebmVO = new EbmVO();
        //ebmVO.setAPN("123");
        Map<String,Object> params = new HashMap<>();
        params.put("java.lang.String","123");
        String b = getInvokeResult(ebmVO,methodName,params);
        System.out.println(b);
    }

}
